import btree4j.BTreeCallback;
import btree4j.BTreeException;
import btree4j.BTreeIndexDup;
import btree4j.Value;
import btree4j.indexer.IndexQuery;
import btree4j.indexer.BasicIndexQuery;
import java.io.File;

public class MultiColumnBTreeTest {

    public static void main(String[] args) throws Exception {
        // 创建一个测试文件
        File testFile = new File("btree_test.idx");
        if (testFile.exists()) {
            testFile.delete();
        }

        // 创建 B+ 树索引
        BTreeIndexDup index = new BTreeIndexDup(testFile);

        // 测试数据
        String[] names = {"Alice", "Bob", "Charlie", "David", "Eve"};
        int[] ages = {25, 30, 35, 40, 45};
        String[] cities = {"New York", "London", "Paris", "Tokyo", "Sydney"};

        // 插入数据
        for (int i = 0; i < names.length; i++) {
            // 主键是 name 和 age 的组合
            Value primaryKey = new Value((names[i] + ":" + ages[i]).getBytes());
            Value data = new Value((cities[i] + ":" + i).getBytes());
            long ptr = index.addValue(primaryKey, data);
            System.out.println("Inserted: " + primaryKey + " -> " + data + " (ptr: " + ptr + ")");
        }

        // 查询数据
        System.out.println("\nQuerying data:");
        for (String name : names) {
            Value primaryKey = new Value((name + ":30").getBytes());
            IndexQuery query = new BasicIndexQuery(BasicIndexQuery.EQ, primaryKey);
            index.search(query, new BTreeCallback() {
                @Override
                public boolean indexInfo(Value key, long pointer) {
                    try {
                        byte[] data = index.retrieveTuple(pointer);
                        System.out.println("Found: " + key + " -> " + new Value(data));
                        return true;
                    } catch (BTreeException e) {
                        e.printStackTrace();
                        return false;
                    }
                }

                @Override
                public boolean indexInfo(Value key, byte[] value) {
                    return false;
                }
            });
        }

        // 更新数据
        System.out.println("\nUpdating data:");
        Value primaryKeyUpdate = new Value(("Bob:30").getBytes());
        Value newData = new Value("Updated:London:100".getBytes());
        long ptrUpdate = index.addValue(primaryKeyUpdate, newData);
        System.out.println("Updated: " + primaryKeyUpdate + " -> " + newData + " (ptr: " + ptrUpdate + ")");

        // 删除数据
        System.out.println("\nDeleting data:");
        Value primaryKeyDelete = new Value(("Charlie:35").getBytes());
        long removedPtr = index.removeValue(primaryKeyDelete);
        System.out.println("Deleted: " + primaryKeyDelete + " (ptr: " + removedPtr + ")");

        // 再次查询数据
        System.out.println("\nQuerying data after update and delete:");
        for (String name : names) {
            Value primaryKey = new Value((name + ":30").getBytes());
            IndexQuery query = new BasicIndexQuery(BasicIndexQuery.EQ, primaryKey);
            index.search(query, new BTreeCallback() {
                @Override
                public boolean indexInfo(Value key, long pointer) {
                    try {
                        byte[] data = index.retrieveTuple(pointer);
                        System.out.println("Found: " + key + " -> " + new Value(data));
                        return true;
                    } catch (BTreeException e) {
                        e.printStackTrace();
                        return false;
                    }
                }

                @Override
                public boolean indexInfo(Value key, byte[] value) {
                    return false;
                }
            });
        }

        // 关闭索引
        index.close();
    }
}